//
//  CameraViewController.m
//  webApp
//
//  Created by 黄晓容 on 16/5/29.
//  Copyright © 2016年 cloud. All rights reserved.
//

#import "CameraViewController.h"

@interface CameraViewController()

@property (nonatomic,strong) UIButton *cameraBtn;

@property (nonatomic,strong) CameraView *cameraView;

@property (nonatomic) CameraType cameraType;

@end

@implementation CameraViewController

-(instancetype)initWithPositionDevice:(CameraType)cameraType
{
    if (self = [super init]) {
        _cameraType = cameraType;
    }
    return self;
}

- (instancetype)init
{
    if(self = [self initWithPositionDevice:CameraTypePostposition]){
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configViews];
}

#pragma mark - 配置视图
- (void)configViews
{
    [self.view addSubview:self.cameraView];
    [self.view addSubview:self.cameraBtn];
}

- (CameraView *)cameraView
{
    if (!_cameraView) {
        _cameraView = [[CameraView alloc] initWithFrame:self.view.bounds positionDevice:self.cameraType];
    }
    return _cameraView;
}

- (UIButton *)cameraBtn
{
    if (!_cameraBtn) {
        UIImage *image = [UIImage imageNamed:@"shot"];
        CGSize imageSize = image.size;
        CGFloat x = (self.view.bounds.size.width - imageSize.width) /2 ;
        CGFloat y = self.view.bounds.size.height - imageSize.height - 20;
        CGRect frame = CGRectMake(x, y, imageSize.width, imageSize.height);
        _cameraBtn = [[UIButton alloc] initWithFrame:frame];
        [_cameraBtn setBackgroundImage:image forState:UIControlStateNormal];
        [_cameraBtn setTitle:@"拍摄" forState:UIControlStateNormal];
        [_cameraBtn addTarget:self action:@selector(capturePhoto) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cameraBtn;
}

#pragma mark - action
- (void)capturePhoto
{
    [self.cameraView capturePhoto:^(UIImage *image) {
        !_cameraCaptureImage?:_cameraCaptureImage(image);
    }];
}

@end
